package com.jt.ktdemo.ui.messsageDetail

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.jt.ktdemo.R
import com.jt.ktdemo.adapter.OnItemClickListener
import com.jt.ktdemo.adapter.ReportMessageDetailAdapter
import com.jt.ktdemo.adapter.SystemMessageDetailAdapter
import com.jt.ktdemo.bean.ObtionCallbackBean
import com.jt.ktdemo.bean.RcMessage
import com.jt.ktdemo.databinding.ActivityMessageDetailBinding
import com.jt.ktdemo.http.HTTPAPI
import com.jt.ktdemo.listener.OnItemSysClickListener
import com.jt.ktdemo.ui.web.WebActivity
import com.jt.ktdemo.utils.GsonUtils
import com.jt.ktdemo.utils.LogUtil
import com.lzy.okgo.callback.StringCallback
import kotlinx.android.synthetic.main.activity_message_detail.*
import okhttp3.Call
import okhttp3.Response
import java.lang.Exception

class MessageDetailActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMessageDetailBinding
    private var listStrs: ArrayList<RcMessage> = ArrayList()
    private var mSystemMessageDetailAdapter: SystemMessageDetailAdapter? = null
    private var mReportMessageDetailAdapter: ReportMessageDetailAdapter? = null

    private lateinit var title: String


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_message_detail)
        binding.activity = this
        initView()
    }

    private fun initView() {
        binding.ivModuleBack.setOnClickListener {
            finish()
        }

        //标题
        title = intent.getStringExtra("module_title").toString()
        if (!title.isBlank()) {
            if("System messages".equals(title)) {
                binding.rlModule.layoutManager = LinearLayoutManager(this)
                mSystemMessageDetailAdapter = SystemMessageDetailAdapter(R.layout.info,listStrs,this)
                binding.rlModule.adapter = mSystemMessageDetailAdapter
                mSystemMessageDetailAdapter!!.setOnItemSysClickListener(object:
                    OnItemSysClickListener {
                    override fun setOnItemSysClickListener(url: String,title:String) {
                        var intent : Intent = Intent(this@MessageDetailActivity,WebActivity().javaClass)
                        intent.putExtra("module_url",url)
                        intent.putExtra("module_title",title)
                        startActivity(intent);
                    }
                })
                tvModuleTitle.text = "System infomation"
                getSystemMessageList(1)
            }else if ("Report messages".equals(title)){
                tvModuleTitle.text = "Report message"
                binding.rlModule.layoutManager = LinearLayoutManager(this)
                mReportMessageDetailAdapter = ReportMessageDetailAdapter(R.layout.report_info,listStrs,this)
                binding.rlModule.adapter = mReportMessageDetailAdapter
                getReportMessageList(1)
            }
        }
    }

    /**
     * 报告消息
     */
    fun getReportMessageList(page : Int) {
        HTTPAPI.getReportMessageList(page,object : StringCallback() {
            @SuppressLint("NotifyDataSetChanged")
            override fun onSuccess(t: String?, call: Call?, response: Response?) {
                LogUtil.e("报告消息-->$t")
                val bean: ObtionCallbackBean =
                    GsonUtils.parse(t.toString(), ObtionCallbackBean::class.java)
                if (!bean.IsError) {
                    listStrs.clear()
                    listStrs.addAll(bean.rcMessages)
                    mReportMessageDetailAdapter?.notifyDataSetChanged()
                }
                getSystemMessageListSecond(1)
            }

            override fun onError(call: Call?, response: Response?, e: Exception?) {
                LogUtil.e("报告消息失败-->${e!!.message}")

            }
        })
    }

    /**
     * 系统消息
     */
    fun getSystemMessageListSecond(page : Int) {
        HTTPAPI.getSystemMessageList(page,object : StringCallback() {
            @SuppressLint("NotifyDataSetChanged")
            override fun onSuccess(t: String?, call: Call?, response: Response?) {
                LogUtil.e("系统消息-->$t")
                var bean: ObtionCallbackBean =
                    GsonUtils.parse(t.toString(), ObtionCallbackBean::class.java)
                if (!bean.IsError) {
                    listStrs.addAll(bean.rcMessages)
                    mReportMessageDetailAdapter?.notifyDataSetChanged()
                }
            }

            override fun onError(call: Call?, response: Response?, e: Exception?) {
                LogUtil.e("系统消息失败-->${e!!.message}")
            }
        })
    }

    /**
     * 系统消息
     */
    fun getSystemMessageList(page : Int) {
        HTTPAPI.getSystemMessageList(page,object : StringCallback() {
            @SuppressLint("NotifyDataSetChanged")
            override fun onSuccess(t: String?, call: Call?, response: Response?) {
                LogUtil.e("系统消息-->$t")
                var bean: ObtionCallbackBean =
                    GsonUtils.parse(t.toString(), ObtionCallbackBean::class.java)
                if (!bean.IsError) {
                    listStrs.addAll(bean.rcMessages)
                    mSystemMessageDetailAdapter?.notifyDataSetChanged()
                }
            }

            override fun onError(call: Call?, response: Response?, e: Exception?) {
                LogUtil.e("系统消息失败-->${e!!.message}")
            }
        })
    }
}